var menuIds;
$(function() {
	getMenuTreeData();
	//validateRule();
});
/*$.validator.setDefaults({
	submitHandler : function() {
		getAllSelectNodes();
		update();
	}
});*/
function loadMenuTree(menuTree) {
	$('#menuTree').jstree({
		"plugins" : [ "wholerow", "checkbox" ],
		'core' : {
			'data' : menuTree
		},
		"checkbox" : {
			//"keep_selected_style" : false,
			//"undetermined" : true
			//"three_state" : false,
			//"cascade" : ' up'
		}
	});
	$('#menuTree').jstree('open_all');
}
function getAllSelectNodes() {
	var ref = $('#menuTree').jstree(true); // 获得整个树
	menuIds = ref.get_selected(); // 获得所有选中节点的，返回值为数组
	$("#menuTree").find(".jstree-undetermined").each(function(i, element) {
		menuIds.push($(element).closest('.jstree-node').attr("id"));
	});
	console.log(menuIds); 
}
function getMenuTreeData() {
	var id = $('#id').val();
	$.ajax({
		type : "post",
		url : "/menuTree?roleId=" + id,
		success : function(data) {
			loadMenuTree(data);
		}
	});
}

function update() {
	getAllSelectNodes();
	var id = $('#id').val();
	console.log(menuIds);
	$.ajax({
		cache : true,
		type : "POST",
		url : "/modifyRoleMenus",
		data : {
			"id":id,
			"menuIds":menuIds
		}, // 你的formid
		async : false,
		traditional: true,
		error : function(requezzst) {
			alert("Connection error");
		},
		success : function(r) {
			if (r.code == 200) {
				parent.layer.msg("分配成功");
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.msg(r.msg);
			}

		}
	});
}
